/*
*   OBD数据: 水温、油量、车速、转速、电压、CAN总线速度检测、CAN总线唤醒
*   功能：蜂鸣器、OTA、看门狗、三个解析函数(角度解析)、背光控制、VIN输入电压高低报警
*/
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "nvs.h"
#include "esp_log.h"
#include "app_data.h"
#include "app_ble.h"
#include "bsp_adc.h"
#include "bsp_sleep.h"
#include "bsp_backlight.h"

static uint32_t last_hb_time = 0;
static uint32_t last_ns_time = 0;
static void app_can_data_analysis(void);
static void app_ble_read_analysis(void);

void app_data_task(void *arg)
{

    while (1)
    {
        //阻塞的线程
        /**从各个外设获取数据**/
        // bsp_ble_read(app_data.ble_data);
        //bsp_can_read(app_data.can_data);
        // bsp_adc_read(app_data.adc_data);

        /**解析数据**/
        app_ble_read_analysis();
        // app_can_data_analysis(app_data);
        // app_adc_data_analysis(app_data);
        if (app_data.light_mode == AUTO)
        {
            bsp_backlight_calculate_set(app_data.sensor.chip_temp, app_data.sensor.light, app_data.light); //计算背光强度
        }
        else
        {
            app_data.backlight_duty = (app_data.light > 100) ? 100 : app_data.light;
            bsp_backlight_set_duty((float)app_data.backlight_duty);
        }
    }
}

/* 判断是否为疲劳驾驶 */
uint8_t app_tired_judge(void)
{
    if (app_tick - app_data.engine_start_time > 1440000)
    {
        return 1;
    }

    return 0;
}

static void app_can_data_analysis(void)
{
    //obd数据校验
    if ((app_data.can_data[0] == 0x03 || app_data.can_data[0] == 0x04) && (app_data.can_data[1] == 0x41))
    {
        switch (app_data.can_data[2])
        {
        case 0x05:
            app_data.obd.water = (int16_t)app_data.can_data[3] - 40;
            break;
        case 0x2f:
            app_data.obd.gas = (uint8_t)((uint16_t)app_data.can_data[3] * 100 / 255);
            break;
        case 0x0d:
            app_data.obd.speed = app_data.can_data[3];
            break;
        case 0x42:
            app_data.obd.voltage = (uint8_t)(((uint16_t)app_data.can_data[3] * 256 + (uint16_t)app_data.can_data[4]) / 100);
            break;
        case 0x0c:
            app_data.obd.rotation = (uint16_t)(((uint16_t)app_data.can_data[3] * 256 + (uint16_t)app_data.can_data[4]) / 4);
            app_data.obd.rotation_enable = 1;
            break;
        default:
            break;
        }
    }
}

void app_adc_read_analysis(void)
{
    /* 可在此处进行滤波 */

    int adc_buffer[ADC_CHANNEL_NUM];

    for (uint8_t i = 0; i < ADC_CHANNEL_NUM - 1; i++)
    {
        adc_buffer[i] = bsp_adc_read(i);
    }

    app_data.sensor.vin = adc_buffer[ADC_VIN] * 0.007568359375;
    app_data.sensor.chip_temp = adc_buffer[ADC_CHIP_TEMP] / 10.0;   //不会换算
    app_data.sensor.board_temp = adc_buffer[ADC_BOARD_TEMP] / 10.0; //使用传感器公式
    app_data.sensor.light = adc_buffer[ADC_LIGHT] / 10.0;           //计算公式
    app_data.sensor.angle = adc_buffer[ADC_ANGLE] / 10.0;           //计算公式
}

static navigate_data_t app_ble_data_read(void)
{
    navigate_data_t nav_data;

    if (navigate_msg_queue != NULL && xQueueReceive(navigate_msg_queue, &(nav_data), /*(TickType_t)10*/ portMAX_DELAY) == pdPASS)
    {
        return nav_data;
    }

    return nav_data;
}

static void app_ble_read_analysis(void)
{
    // 还是得用tick计时判断是否退出导航
    /* 阻塞的接收函数 可建立线程*/
    /* OTA升级指令部分 来电显示部分*/
    static uint8_t clear_buffer_flag = 0;
    navigate_data_t data = app_ble_data_read();

    switch (data.value[6])
    {
    case NAV_INFO:
        last_ns_time = app_tick;
        app_data.nav.enable = 1;
        app_data.nav.speed = data.value[17];
        app_data.nav.speed_limit = data.value[12];
        // app_data.nav.speed_limit = 100;
        app_data.nav.camera = data.value[13];
        switch (data.value[7])
        {
        case FRONT:
            app_data.nav.next_dir = 0;
            break;
        case CHANGE_RIGHT:
            app_data.nav.next_dir = 2;
            break;
        case CHANGE_LEFT:
            app_data.nav.next_dir = 1;
            break;
        case TURN_RIGHT:
            app_data.nav.next_dir = 4;
            break;
        case TURN_LEFT:
            app_data.nav.next_dir = 3;
            break;
        case BACK_RIGHT:
            app_data.nav.next_dir = 6;
            break;
        case BACK_LEFT:
            app_data.nav.next_dir = 5;
            break;
        default:
            app_data.nav.next_dir = 0;
            break;
        }
        app_data.nav.next_dist = (data.value[8] << 24) | (data.value[9] << 16) | (data.value[10] << 8) | data.value[11];
        break;
    case NAV_STATUS:
        last_ns_time = app_tick;
        app_data.nav.enable = data.value[7];
        break;
    case HEART_BEAT:
        last_hb_time = app_tick;
        break;
    case BRIGHT_MANUAL:
        if (app_data.light != data.value[7])
        {
            app_data.light = data.value[7];
            bsp_sleep_save_data(SAVE_BL);
        }

        if (app_data.light_mode != 1)
        {
            app_data.light_mode = 1;
            bsp_sleep_save_data(SAVE_BL_MODE);
        }
        break;
    case BRIGHT_AUTO:
        if (app_data.light != data.value[7])
        {
            app_data.light = data.value[8] * 5;
            bsp_sleep_save_data(SAVE_BL);
        }

        if (app_data.light_mode != 0)
        {
            app_data.light_mode = 0;
            bsp_sleep_save_data(SAVE_BL_MODE);
        }
        break;
    case UPDATE_FIRMWARE:
        app_data.update_cmd = 1;
    default:
        break;
    }

    //断开后数据清零
    if (!app_data.nav.enable || !app_data.ble_enable)
    {
        if (clear_buffer_flag == 0)
        {
            printf("clear buffer! nav:%d ble:%d\r\n", app_data.nav.enable, app_data.ble_enable);
            memset(&app_data.nav, 0, sizeof(nav_info_t));
            clear_buffer_flag = 1;
        }
    }
    else if (app_data.nav.enable == 1 && app_data.ble_enable == 1)
    {
        clear_buffer_flag = 0;
    }

    if (!app_data.obd.enable)
    {
        memset(&app_data.obd, 0, sizeof(obd_info_t));
    }
}
